﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Esb.Soa.Domain.Monitor
{
    public class ServiceMonitor
    {
        public int Time1;
        public int Time2;
        public int Time3;

        /// <summary>
        /// 服务调用开始时间
        /// </summary>
        private DateTime startTime
        {
            get;
            set;
        }

        /// <summary>
        /// 服务调用结束时间
        /// </summary>
        private DateTime endTime
        {
            get;
            set;
        }

        private Exception exception
        {
            get;
            set;
        }

        /// <summary>
        /// 服务+方法全名
        /// </summary>
        public string ServiceFullName
        {
            get;
            private set;
        }

        public string ServiceEntity
        {
            get;
            set;
        }

        public string ServiceMethod
        { 
            get; 
            set; 
        }

        /// <summary>
        /// 调用方Id
        /// </summary>
        public int CallerId
        {
            get;
            private set;
        }

        public int ServiceApplicationId
        {
            get;
            set;
        }

        /// <summary>
        /// 往返数据流量
        /// </summary>
        public int TraficRequest { get; set; }
        public int TraficReponse { get; set; }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="appId"></param>
        public ServiceMonitor(string serviceFullName, int callerId)
        {
            this.ServiceFullName = serviceFullName;
            this.CallerId = callerId;
        }

        /// <summary>
        /// 统计服务的调用起止时间和是否异常的信息
        /// </summary>
        public void RecordServiceCall()
        {
            try
            {
                int TotalMilliSeconds = (int)(this.endTime - this.startTime).TotalMilliseconds;

                //如果服务异常则告警
                if (!IsSuccess)
                {
                    //ServiceAlert alert = new ServiceAlert();
                    //alert.SetAlertNumber(this.ServiceName);
                    //if(alert.GetAlertNumber(this.ServiceName) >= ServiceAlert.AlertNumberThreshhold)
                    //{
                    //    alert.SendAlertEmail();
                    //}

                    if (this.exception != null)
                    {
                        //Logger.Info("Esb Error", this.ServiceName + "|" + TotalMilliSeconds + "|" + TraficRequest + "|" + TraficReponse, this.exception.Message);
                        //格式：服务应用名.服务实体名.方法名
                    }
                    else
                    {
                        //Logger.Info("Esb Error", this.ServiceName + "|" + TotalMilliSeconds + "|" + TraficRequest + "|" + TraficReponse, "服务尚未注册或不存在");
                    }
                }
                else
                {
                    //TODO:预留调用BI，发送服务执行时间等的统计数据
                    //Logger.Info("Esb Call", this.ServiceName + "|" + TotalMilliSeconds + "|" + TraficRequest + "|" + TraficReponse, "服务ApplicationId：" + this.ServiceApplicationId);
                }
            }
            catch
            {

            }
        }

        public void SetStartTime()
        {
            this.startTime = DateTime.Now;
        }

        public void SetEndTime()
        {
            this.endTime = DateTime.Now;
        }

        /// <summary>
        /// 服务是否调用成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 记录异常信息
        /// </summary>
        /// <param name="exception"></param>
        public void SetException(Exception exception)
        {
            this.exception = exception;
        }
    }
}
